package com.soujiayi.activity;


import com.soujiayi.net.NetworkUtils;
import com.soujiayi.view.NetworkUnavailableDialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ProductCommentActivity extends Activity implements TextWatcher{
	
	private EditText comment;
	private TextView errorMessage;
	
	private LinearLayout contentLayout;
	private NetworkUnavailableDialog networkDialog; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.product_comment);
		
		if(!NetworkUtils.isNetworkConnected(this)){
			contentLayout = (LinearLayout) findViewById(R.id.product_comment_content);
			contentLayout.setVisibility(8);
			networkDialog = new NetworkUnavailableDialog(this, R.style.networkDialog);
			networkDialog.show();
			return;
		}
		
		comment = (EditText)findViewById(R.id.product_comment);
		comment.addTextChangedListener(this);
		
		errorMessage = (TextView)findViewById(R.id.product_comment_errorMessage);
	}
	
	
	public void backToHome(View view){
		Intent intent = new Intent(ProductCommentActivity.this, IndexActivity.class);
		startActivity(intent);
		this.finish();
	}
	
	public void back(View view){
		this.finish();
		overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//如果按下的是返回键，并且没有重复
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
			return false;
		}
		return false;
	}

	public void submit(View view){
		if(comment.getText() == null || "".equals(comment.getText().toString().trim())){
			errorMessage.setVisibility(0);
		}else{
			this.finish();
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		errorMessage.setVisibility(8);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		
	}
}
